package pumpkin.ast;

import pumpkin.evt.Environment;
import pumpkin.word.Token;
import pumpkin.word.TokenSource;
import pumpkin.word.TokenType;

public class Id  implements Node {
	String txt;
	private boolean isNumber=false;
	@Override
	public void build(TokenSource tokenSource) {
		Token t=tokenSource.next();
		if(t.getType()==TokenType.NUMBER) {
			isNumber=true;
		}else {
			isNumber=false;
		}
		txt=t.getText();
	}
	public void build(Token tk) {
		if(tk.getType()==TokenType.ID) {
			txt=tk.getText();
			System.out.println("id->"+txt);
			isNumber=false;
		}
		if(tk.getType()==TokenType.NUMBER) {
			txt=tk.getText();
			System.out.println("num->"+txt);
			isNumber=true;
		}
	}
	@Override
	public Object excute(Environment env) {
		if(isNumber) {
			if(txt.contains(".")) {
				return Double.parseDouble(txt);
			}else{
				return Integer.parseInt(txt);
			}
		}
		return env.get(txt);
	}

}
